<?php
class packageControl extends Control{
    //安装包列表显示
    public function list_show(){
        $_GET['sm']=1;
        $_GET['menu']=1;
        $_GET['aid']=isset($_GET['aid'])?(int)$_GET['aid']:0;//默认显示全部列表
        if($_GET['aid']==0){$where='true';}else{$where=" aid={$_GET['aid']}";}
        $db=M('appcate');
        $package=$db->table('package');
        $packview=K('index/packageview');
        $page=new page($package->where($where)->count(), 2, 2,4);
        $result=$packview->limit($page->limit())->field('id,chname,cname,username,addtime,volume')->join("packcate|user")->all($where);
        $this->assign("result", $result);
        $this->assign("alist", $db->all());
        $this->assign("pageshow", $page->show());
        $this->display();
    }
    //上传界面
    public function add_show(){
        $_GET['sm']=2;
        $_GET['menu']=1;
        $db=M('appcate');
        $applist=$db->all();
        $packlist=$db->table('packcate')->all();
        $verlist=$db->table('version')->all();
        $sizelist=$db->table('size')->all();
        $this->assign("slist", $sizelist);
        $this->assign("vlist", $verlist);
        $this->assign("alist", $applist);
        $this->assign("plist", $packlist);
        $this->display();
    }
    //上传
    public function add(){
        $db=M('package');
        $_POST['uid']=22;
        $_POST['logo']=  isset($_POST['logo'])?$_POST['logo'][1][0]:'';
        $_POST['code']=  isset($_POST['code'])?$_POST['code'][1][0]:'';        
        $_POST['savepath']=  isset($_POST['savepath'])?$_POST['savepath'][1][0]:'';
        if(isset($_POST['screenshot'])){
            $arr=array();
            foreach ($_POST['screenshot'] as $s) {
                array_push($arr, $s[0]);
            }
            $_POST['screenshot']=  implode('@@', $arr);
        }
        if(!$db->add())exit;
        $lastid=$db->get_insert_id();
        if(isset($_POST['version'])){
            $version=array();
            foreach ($_POST['version'] as $v) {
                $version['vid']=$v;
                $version['id']=$lastid;
                $db->table('package_version')->add($version);
            }
        }
        if(isset($_POST['size'])){
            $size=array();
            foreach ($_POST['size'] as $si) {
                $size['sid']=$si;
                $size['id']=$lastid;
                $db->table('package_size')->add($size);
            }
        }
        go(__CONTROL__.'/add_show');
    }
    //删除
    public function del(){
        $db=k('packagerelation');
        if(isset($_GET['id'])){
            if($db->del("id={$_GET['id']}")){
                go(__WEB__."/houtai/packlist");
            }
        }
    }
    //编辑界面
    public function edit_show(){
        
    }
    //编辑
    public function edit(){
        
    }
    public function select(){
        $db=M('packcate');
        if(!isset($_POST['aid'])){go(__CONTROL__.'/add_show');}
        $row=$db->all("pid={$_POST['aid']}");
        echo json_encode($row);
    }
}
?>
